# coding:utf-8
# writingtime: 
from Utilities.Plot.SimplePlot import SimpleClass
from DataAnalyze.ScoreFuntionAnalyze.getDataFunction import GetTestData

class ScoreAnalyze:
    def __init__(self, scorefunc, datafunc, imgshow=False, imgsaving=False):
        """
        function: 得分函数测试
        :param scorefunc: 得分函数，例如<function getLineRandom at 0x0000021CDDA93670>
        :param datafunc: 用作测试的数据集
        :param imgshow:展示图片
        :param imgsaving: 保存图片
        """
        self.scorefunc = scorefunc
        self.datafunc = datafunc
        self.data = datafunc()
        self.imgshow = imgshow
        self.imgsaving = False

    def qscan(self):
        """
        function: 找到满足区间值广义正交定义的q值
        :return: q
        """
        q = 1
        for i in self.data:
            for k in i:
                if k[0][0] == 0:
                    k[0][0] = k[0][0] + 0.00001
                if k[1][0] == 0:
                    k[1][0] = k[0][0] + 0.0001
                if k[0][1] == 0:
                    k[0][1] = k[0][0] + 0.01
                if k[1][1] == 0:
                    k[1][1] = k[0][0] + 0.01
                count = 0
                while (k[0][0] ** q + k[1][0] ** q >= 1) or (k[0][1] ** q + k[1][1] ** q >= 1):
                    q += 1
                    if count < 20:
                        count = count + 1
                    else:
                        break
        return q

    def getresult(self):
        """
        function: 将得分函数进行比较
        :return:
        """
        data_list = self.data
        list1 = []
        list2 = []
        q = self.qscan()
        # q=3
        print('q的取值为：', q)
        # print(data_list)
        for i in data_list:
            socre1 = self.scorefunc(i[0], q)
            socre2 = self.scorefunc(i[1], q)
            list1.append(socre1)
            list2.append(socre2)
        SimpleClass().qSensitivity([list1, list2], [1 + i for i in range(len(list1))], y_label="Score",
                                   title=self.scorefunc.__name__,imgShow=self.imgshow,imgSaving=self.imgsaving)

    @staticmethod
    def analyze(func,dataname):
        """

        :param func: 得分函数名称
        :param dataname: 数据名称
        :return:
        """
        datafun = GetTestData.getfun()
        fun1 = datafun[dataname]
        ScoreAnalyze(func, fun1, imgshow=True).getresult()

if __name__ == "__main__":
    from DataAnalyze.ScoreFuntionAnalyze.scoreFunction import f0
    ScoreAnalyze.analyze(f0,'getZeroRandomIVq_ROF')

